package com.example.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.R;
import com.example.model.pojo.*;
import com.example.model.vo.MaterialsVo;
import com.example.service.InFormService;
import com.example.service.MaterialsService;
import com.example.service.OutFormService;
import com.example.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 入库控制类
 */
@RestController
@RequestMapping("outWarehouse/outForm/")
public class OutFormController {

    @Autowired(required = false)
    MaterialsService materialsService;

    @Autowired(required = false)
    OutFormService outFormService;

    @Autowired(required = false)
    InFormService inFormService;

    /**
     * 获取所有物资信息
     */
    @RequestMapping("getAllMaterialsVo")
    public R<List<MaterialsVo>> getAllMaterialsVo(){
        List<MaterialsVo> materialsVoList=materialsService.getAllVo();
        return R.success(materialsVoList);
    }

    /**
     * 提交出库审核单
     */
    @RequestMapping("submitOutForm")
    public R<String> getMaterialsVo(@RequestBody OutForm outForm){
        outForm.setMyId(null);
        outForm.setDate(new Date());
        Boolean flag = outFormService.addOutForm(outForm);

        if (!flag){
            return R.error("添加失败");
        }
        return R.success("添加成功");
    }

    /**
     * 获取待审核表单
     */
    @RequestMapping("getUnreviewedPage")
    public R<PageResult> getUnreviewedPage(@RequestBody Map<String,String> map){
        String pageStr = map.get("current");
        String pageSizeStr = map.get("size");
        String outFormId = map.get("outFormId");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
//        若为空，则查询第一页，每页10条
        if (StringUtils.isBlank(pageStr)){
            pageStr="1";
        }
        if (StringUtils.isBlank(pageSizeStr)){
            pageSizeStr="10";
        }
        Integer page = Integer.parseInt(pageStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);



        PageResult pageResult = outFormService.getUnreviewedPage(page,pageSize,outFormId,startDate,endDate);
        return R.success(pageResult);
    }

    /**
     * 提交审核结果
     */
    @RequestMapping("audit")
    public R<String> audit(@RequestBody OutForm outForm){

        Boolean flag = outFormService.audit(outForm);
        if (!flag){
            return R.error("审核失败");
        }

        return R.success("审核完成");
    }


}
